package io.renren.modules.live.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import io.renren.common.utils.OrderCodeUtil;
import io.renren.modules.live.dto.orderDto;
import io.renren.modules.live.entity.FanweVideoPropEntity;
import io.renren.modules.live.util.RabbitUtil;
import io.renren.modules.live.vo.orderVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.live.dao.FanweOrderDao;
import io.renren.modules.live.entity.FanweOrderEntity;
import io.renren.modules.live.service.FanweOrderService;
import org.springframework.transaction.annotation.Transactional;

@Log4j2
@Service("fanweOrderService")
public class FanweOrderServiceImpl extends ServiceImpl<FanweOrderDao, FanweOrderEntity> implements FanweOrderService {

    @Autowired
    private FanweOrderDao fanweOrderDao;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 生成订单
     * @throws IOException
     */
    @RabbitListener(queues = "exchange.order.bonus")
    public void consume(Message message,Channel channel) throws IOException {
        //FanweVideoPropEntity propEntity = (FanweVideoPropEntity) rabbitTemplate.receiveAndConvert("exchange.order.bonus");
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);//手动确认
        String msgJson = new String(message.getBody(),"UTF-8");
        byte[] body = message.getBody();
        String json = new String(body,"UTF-8");
        JSONObject jsonObject1 = JSONObject.parseObject(json);
        Long userId = jsonObject1.getLong("userId");
        BigDecimal propMoney = jsonObject1.getBigDecimal("propMoney");
        log.info("收到消息: {}", message.toString());
        //生成订单保存到数据库
        FanweOrderEntity orderEntity = new FanweOrderEntity();
        String againCode = OrderCodeUtil.getAgainCode(userId);//生成订单号
        orderEntity.setOrderNumber(againCode);
        orderEntity.setOrderUserType(1);
        orderEntity.setOrderUserId(userId);
        orderEntity.setOrderType(1);
        orderEntity.setOrderMoney(propMoney);
        orderEntity.setOrderMode("1");
        orderEntity.setCreateTime(new Date());
        fanweOrderDao.save(orderEntity);

    }



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FanweOrderEntity> page = this.page(
                new Query<FanweOrderEntity>().getPage(params),
                new QueryWrapper<FanweOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<FanweOrderEntity> getUserIdDataOrderlist(Integer userId, String time,Long page,Long size) {
        return fanweOrderDao.getUserIdDataOrderlist(userId,time,page,size);
    }

    /**
     * 充值记录
     * @param orderVo
     * @return
     */
    @Override
    public Map<String, Object> selectOrderListOne(orderVo orderVo) throws  Exception{
        Map<String, Object> map=new HashMap<>();
        orderVo.setPageIndex((orderVo.getPageIndex()-1)*orderVo.getPageSize());
        orderDto orderDto = fanweOrderDao.selectOrderListOne(orderVo);
        Integer integer = fanweOrderDao.selectOrderListOneCount(orderVo);
        map.put("orderDto",orderDto);
        map.put("count",integer);
        return map;
    }

    /**
     * 消费记录
     * @param orderVo
     * @return
     */
    @Override
    public Map<String, Object> selectOrderListTwo(orderVo orderVo) throws   Exception{
        Map<String, Object> map=new HashMap<>();
        orderVo.setPageIndex((orderVo.getPageIndex()-1)*orderVo.getPageSize());
        orderDto orderDto = fanweOrderDao.selectOrderListTwo(orderVo);
        Integer integer = fanweOrderDao.selectOrderListTwoCount(orderVo);
        map.put("orderDto",orderDto);
        map.put("count",integer);
        return map;
    }


}